﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BF.IBLL.BaseSystem.AreaManagement;
using BF.Model;
using WebDataControls;
using Base.Core.UnityFactory;
using System.Configuration;
using BF.Infrastructure;
using System.Web.Security;
using System.Web.UI.HtmlControls;

namespace BF.Web.pages.BaseSystem.AreaManagement
{
    public partial class AreaList : BasePage
    {
        private readonly Isys_AreaBLL iBLL = IOCFactory.GetIOCResolve<Isys_AreaBLL>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SysAreaTree1.RootAreaCode = "340000";
                dic_Organization model = GetSessionCurrentUserInfo.dic_Organization;
                if (model == null)
                {
                    //SysAreaTree1.RootAreaCode = GetSysParam("AreaCode");
                    //SysAreaTree1.SelectedAreaCode = GetSysParam("AreaCode");
                   // SysAreaTree1.SelectedAreaCode = GetSysParam("AreaCode");
                   // AreaControl.SetSysAreaValue(GetSysParam("AreaCode"));
                }
                else
                {
                    SysAreaTree1.RootAreaCode = model.sAreaCode;
                    SysAreaTree1.SelectedAreaCode = model.sAreaCode;

                   // AreaControl.SetSysAreaValue(model.sDivisionCode);
                }
                //AreaControl.SetSysAreaValue(GetSysParam("AreaCode"));
               // AreaControl.SetHideControl(5);
                this.AspNetPager1.CurrentPageIndex = 1;
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {

            this.AspNetPager1.CurrentPageIndex = 1;
            DataRepeater(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex);
        }
        /// <summary>
        /// 分页按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            DataRepeater(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex);
        }

        /// <summary>
        /// 查询按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtnQuery_Click(object sender, EventArgs e)
        {
            DataRepeater(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex);
        }


        /// <summary>
        /// 停用或启用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtnStop_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < Repeater1.Items.Count; i++)
            {
                HtmlInputCheckBox cb = (HtmlInputCheckBox)Repeater1.Items[i].FindControl("chksuserid");
                if (cb != null && cb.Checked)
                {
                    sys_Area model = iBLL.GetModelByID(cb.Value);
                    if (model.iFlag == 0)
                    {
                        model.iFlag = 1;
                    }
                    else if (model.iFlag == 1)
                    {
                        model.iFlag = 0;
                    }
                }
            }
            if (iBLL.Update())
            {
                Messages("操作成功!");
            }
            else
            {
                Messages("操作失败!");
            }
            this.AspNetPager1.CurrentPageIndex = 1;

        }

        /// <summary>
        /// 获取数据到Repeater的方法
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="currentPageIndex"></param>
        private void DataRepeater(int pageSize, int currentPageIndex)
        {
            string areaCode = SysAreaTree1.SelectedAreaCode;// AreaControl.AreaCode;SysAreaTree1.Select
            string subAreaCode = SysAreaTree1.SelectedAreaCode;// AreaControl.AreaCode;
            PageModel pm = new PageModel(currentPageIndex);
            List<sys_Area> list = new List<sys_Area>();
           // int flag = this.iFlag.Checked ? 1 : 0;
            if (string.IsNullOrEmpty(areaCode))
            {
                AspNetPager1.RecordCount = iBLL
                    .GetRowCount(
                    p => (string.IsNullOrEmpty(this.AreaQuery.Text.Trim()) ? true : p.sName.Contains(this.AreaQuery.Text.Trim()))
                       // && (p.iFlag == flag)
                       );
                list = iBLL.GetList(
                    p => (string.IsNullOrEmpty(this.AreaQuery.Text.Trim()) ? true : p.sName.Contains(this.AreaQuery.Text.Trim())),
                       //&& (p.iFlag == flag)
                        p => new { p.sID, p.iLevel },
                        pm, true).ToList();
            }
            else
            {
                int lev = iBLL.GetLevelByAreaCode(areaCode) + 1;//当前地区编码级别的子级

                if (!string.IsNullOrEmpty(this.AreaQuery.Text.Trim()))
                {
                    string queryAreaCode = areaCode;
                    if (areaCode.Length == 6)
                    {
                        if (areaCode.Substring(2, 4) == "0000")
                        {
                            queryAreaCode = areaCode.Substring(0, 2);
                        }
                        else if (areaCode.Substring(2, 4) != "0000" && areaCode.Substring(4, 2) == "00")
                        {
                            queryAreaCode = areaCode.Substring(0, 4);
                        }
                    }
                    int ilevel = iBLL.GetLevelByAreaCode(areaCode);

                    AspNetPager1.RecordCount = iBLL.GetRowCount(p => (p.sID.StartsWith(queryAreaCode))
                        && (p.sName.Contains(this.AreaQuery.Text.Trim()))
                        && (p.iLevel > ilevel)
                       // && (p.iFlag == flag)
                        );
                    list = iBLL.GetList(p => (p.sID.StartsWith(queryAreaCode))
                        && (p.sName.Contains(this.AreaQuery.Text.Trim()))
                        && (p.iLevel > ilevel),
                     //   && (p.iFlag == flag)
                            p => new { p.sID, p.iLevel },
                            pm, true).ToList();
                }
                else
                {
                    AspNetPager1.RecordCount = iBLL.GetRowCount(p =>
                        (p.sSuperID == areaCode)
                        && (p.iLevel == lev)
                       // && (p.iFlag == flag)
                        );
                    list = iBLL.GetList(p =>
                        (p.sSuperID == areaCode)
                        && (p.iLevel == lev),
                       // && (p.iFlag == flag),
                            p => new { p.sID, p.iLevel },
                            pm, true).ToList();
                }
            }
            this.Repeater1.DataSource = list;
            this.Repeater1.DataBind();
        }


        ////绑定checkbox
        //protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        //{
        //    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        //    {
        //        HtmlInputCheckBox rb = (HtmlInputCheckBox)e.Item.FindControl("chksuserid");
        //        Label label = (Label)e.Item.FindControl("Label1");
        //        if (rb != null)
        //        {
        //            //把选中行的RowIndex也传过去，提交后在服务器端取值时用
        //            rb.Attributes.Add("onclick", "onClientChange('" + rb.ClientID + "','" + label.Text + "')");
        //        }
        //    }
        //}

    }
}